<template>
  <view class="orders-container">
    <!-- 标题栏 -->
    <view class="header">
      <image 
        class="back-icon" 
        src="/static/icons/back-green.png" 
        mode="widthFix"
        @click="onBack"
      ></image>
      <text class="title">我的预约订单</text>
    </view>

    <!-- Tab 切换 -->
    <view class="tab-bar">
      <view 
        v-for="(tab, index) in tabs" 
        :key="index"
        class="tab-item"
        :class="{ active: currentTab === index }"
        @click="switchTab(index)"
      >
        <text>{{ tab.label }}</text>
      </view>
    </view>

    <!-- 订单列表 -->
    <view class="order-list">
      <view 
        v-for="(order, index) in filteredOrders" 
        :key="index"
        class="order-item"
      >
        <view class="order-info">
          <text class="info-item">预约时间：{{ order.appointTime }}</text>
          <text class="info-item">预约医生：{{ order.doctorName }}</text>
          <text class="info-item">医院地址：{{ order.hospitalAddress }}</text>
        </view>
        <view class="appoint-number">
          <text class="number-label">预约号：</text>
          <text class="number-value" :style="{ color: numberColor }">
            {{ order.appointNumber }}
          </text>
        </view>
      </view>

      <!-- 空数据提示 -->
      <view v-if="filteredOrders.length === 0 && !loading" class="empty-tip">
        <text>暂无预约订单</text>
      </view>

      <!-- 加载中提示 -->
      <view v-if="loading" class="loading-tip">
        <text>加载中...</text>
      </view>
    </view>
  </view>
</template>

<script setup lang="ts">
import { ref, computed, onMounted } from 'vue';

// 定义订单数据类型
interface Order {
  appointTime: string;
  doctorName: string;
  hospitalAddress: string;
  appointNumber: number;
  status: 'all' | 'pending' | 'completed'; // 订单状态
}

// 定义 Tab 类型
interface Tab {
  label: string;
  status: 'all' | 'pending' | 'completed';
}

// 响应式数据
const tabs = ref<Tab[]>([
  { label: '全部', status: 'all' },
  { label: '待预约', status: 'pending' },
  { label: '已完成', status: 'completed' }
]);
const currentTab = ref(0); // 当前激活的 Tab
const orders = ref<Order[]>([]); // 所有订单
const loading = ref(false); // 加载状态
const numberColor = ref('#00cc99'); // 预约号颜色

// 模拟接口请求
const fetchOrders = async () => {
  loading.value = true;
  try {
    // 实际项目中替换为真实接口
    const res = await uni.request({
      url: '/api/orders',
      method: 'GET'
    });
    const data = res.data as { code: number; data: Order[] };
    if (data.code === 200) {
      orders.value = data.data;
    } else {
      uni.showToast({ title: '获取订单失败', icon: 'none' });
    }
  } catch (error) {
    uni.showToast({ title: '网络请求失败', icon: 'none' });
  } finally {
    loading.value = false;
  }
};

// 切换 Tab
const switchTab = (index: number) => {
  currentTab.value = index;
};

// 计算过滤后的订单
const filteredOrders = computed<Order[]>(() => {
  const currentStatus = tabs.value[currentTab.value].status;
  if (currentStatus === 'all') {
    return orders.value;
  }
  return orders.value.filter(order => order.status === currentStatus);
});

// 返回上一页
const onBack = () => {
  uni.navigateBack();
};

// 页面加载时获取数据
onMounted(() => {
  fetchOrders();
});
</script>

<style scoped lang="scss">
/* 容器样式 */
.orders-container {
  background-color: #fff;
  min-height: 100vh;
  padding-bottom: 20px; /* 底部留白 */
}

/* 标题栏样式 */
.header {
  display: flex;
  align-items: center;
  padding: 16px 20px; /* 左右内边距 */
  border-bottom: 1px solid #e5f8ef; /* 浅绿色边框 */

  .back-icon {
    width: 24px;
    height: 24px;
    margin-right: 12px;
  }

  .title {
    font-size: 20px;
    font-weight: bold;
    color: #00cc99; /* 绿色标题 */
  }
}

/* Tab 栏样式 */
.tab-bar {
  display: flex;
  margin: 16px 20px; /* 左右内边距 */
  border-bottom: 1px solid #e5f8ef; /* 浅绿色边框 */

  .tab-item {
    flex: 1;
    text-align: center;
    padding: 10px 0; /* 上下内边距 */
    font-size: 17px;
    color: #999;
    position: relative;

    &::after {
      content: '';
      position: absolute;
      bottom: 0;
      left: 50%;
      width: 0;
      height: 2px;
      background-color: #00cc99;
      transition: all 0.3s ease;
      transform: translateX(-50%);
    }

    &.active {
      color: #00cc99;

      &::after {
        width: 60%; /* 激活时显示下划线 */
      }
    }
  }
}

/* 订单列表样式 */
.order-list {
  padding: 0 20px; /* 左右内边距 */

  .order-item {
    background-color: #f8fff9; /* 浅绿色背景 */
    border-radius: 12px;
    padding: 16px;
    margin-bottom: 16px;
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    box-shadow: 0 2px 8px rgba(0, 204, 153, 0.1); /* 淡绿色阴影 */

    .order-info {
      .info-item {
        display: block;
        font-size: 15px;
        color: #666;
        margin-bottom: 6px;
        line-height: 1.4;
      }
    }

    .appoint-number {
      text-align: right;

      .number-label {
        font-size: 15px;
        color: #999;
      }

      .number-value {
        font-size: 18px;
        font-weight: bold;
        color: #00cc99; /* 绿色预约号 */
      }
    }
  }

  // 空数据提示
  .empty-tip {
    text-align: center;
    padding: 32px 0;
    color: #999;
    font-size: 15px;
  }

  // 加载中提示
  .loading-tip {
    text-align: center;
    padding: 32px 0;
    color: #00cc99;
    font-size: 15px;
  }
}
</style>